Page({
  data: {
    markers: []
  },

  onLoad: function () {
    // 湖州师范固定标注点
    const husnuMarkers = [
      {
        id: 101,
        latitude: 30.8725659513664,
        longitude: 120.12992928923836,
        title: "湖州师范学院图书馆",
        iconPath: "/resources/marker.png",
        width: 30,
        height: 30,
        callout: {
          content: "湖州师范学院图书馆",
          color: "#000000",
          fontSize: 14,
          borderRadius: 6,
          bgColor: "#ffffff",
          padding: 8,
          display: "ALWAYS"
        }
      },
      {
        id: 102,
        latitude: 30.87380197257826,
        longitude: 120.13359420251629,
        title: "德清湖",
        iconPath: "/resources/marker.png",
        width: 30,
        height: 30,
        callout: {
          content: "德清湖",
          color: "#000000",
          fontSize: 14,
          borderRadius: 6,
          bgColor: "#ffffff",
          padding: 8,
          display: "ALWAYS"
        }
      },
      {
        id: 103,
        latitude: 30.876386684177206,
        longitude: 120.13907407046145,
        title: "风雨操场",
        iconPath: "/resources/marker.png",
        width: 30,
        height: 30,
        callout: {
          content: "风雨操场″",
          color: "#000000",
          fontSize: 14,
          borderRadius: 6,
          bgColor: "#ffffff",
          padding: 8,
          display: "ALWAYS"
        }
      },
      {
        id: 104,
        latitude: 30.875435593172,
        longitude: 120.13444989704487,
        title: "信工楼",
        iconPath: "/resources/marker.png",
        width: 30,
        height: 30,
        callout: {
          content: "信息工程学院楼",
          color: "#000000",
          fontSize: 14,
          borderRadius: 6,
          bgColor: "#ffffff",
          padding: 8,
          display: "ALWAYS"
        }
      }
    ];

    // 请求后端动态地点
    wx.request({
      url: 'http://localhost:3000/api/points',
      method: 'GET',
      success: (res) => {
        const dynamicMarkers = res.data.map((point) => ({
          id: Number(point._id),
          latitude: point.location.coordinates[1],
          longitude: point.location.coordinates[0],
          title: point.label,
          iconPath: "/resources/marker.png",
          width: 30,
          height: 30,
          callout: {
            content: point.description,
            color: "#ff0000",
            fontSize: 16,
            borderRadius: 10,
            bgColor: "#ffffff",
            padding: 10,
            display: "ALWAYS"
          }
        }));

        // 合并静态和动态的 markers
        const allMarkers = husnuMarkers.concat(dynamicMarkers);

        // 设置到页面
        this.setData({
          markers: allMarkers
        });
      },
      fail: (err) => {
        console.error("请求失败", err);
        // 即使请求失败，也先展示 husnuMarkers
        this.setData({
          markers: husnuMarkers
        });
      }
    });
  }
});
